import fetch from '@/utils/fetch';
import {hex_md5} from '@/utils/md5';
import qs from 'qs'

const apiUrl = process.env.NODE_ENV == 'development' ? '/api2' : 'http://oa.91118.com:9000/isv' // http://oa.91118.com:6010

const apiName='/apitest/Transport/EventHandle'
const apiImg='/apitest/Transport/UploadFileBase64'
const baseUrl=  process.env.NODE_ENV == 'development' ? 'http://interface.91118.com' : 'http://interface.91118.com'
// const apiName='/api/Transport/EventHandle'
// const apiImg='/api/Transport/UploadFileBase64'

/* GET */
export function axGet(_cmd,_param){
    return fetch({
        baseURL: baseUrl,
        url: apiName,
        method: 'get',
        params: {
            json:urlFun(_cmd,_param)
        },
    })
}
/* GET await*/
export async function axGetAwait(_cmd,_param){
    return await fetch({
        baseURL: baseUrl,
        url: apiName,
        method: 'get',
        params: {
            json:urlFun(_cmd,_param)
        },
    })
}

/* POST */
export function axPost(_cmd,_param){
    return fetch({
        baseURL: baseUrl,
        url: apiName,
        method: 'post',
        data: {
            json:JSON.stringify(urlFun(_cmd,_param))
        },
        transformRequest: [function (data) {
            let ret = ''
            for(let it in data){
                ret += encodeURIComponent(it) + '=' +encodeURIComponent(data[it]) + '&'
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    })
}

/* 图片上传 */
export function axUploadFile(_cmd,_param){
    return fetch({
        baseURL: baseUrl,
        url: apiImg,
        method: 'post',
        data: {
            json:urlFun(_cmd,_param)
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 
        }
    })
}


/* json function */
function urlFun(_cmd,_param){
    var _time = new Date().getTime() + '';
    var _uuid = guid();
    var _noce = noce();
    var _key  = 'HTXFV-FH8YX-VCY69-JJGBK-7R6XP';
    var _appJson={
        "AppId":1,
        "Cmd":_cmd,
        "Ver":"1.0.0",
        "UnixTime":_time,
        "SessionId": _uuid,
        "Noce":_noce,
        "AppKey":_key,
        "SKey":md5('1',_cmd,_time,_noce,_key,_uuid),
        "JsonParts":_param,
    }
    return _appJson;
}

function md5(appid,cmd,unixTime,noce,key,sessionId){
    return  hex_md5("Company=hzty&Ver=1.0.0&Methord=MD5&AppID=" + appid + "&Cmd=" + cmd + "&UnixTime=" + unixTime + "&Noce=" + noce + "&AppKey=" + key + "&SessionId=" + sessionId);
}
function guid() {
    function S4() {
        return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function noce() {
    return parseInt(Math.random() * 100);
}

//通过钉钉的用户code获取师生通用户列表
export function getUserList(data) {
    return fetch({
        url: apiUrl + '/relative/ding-sst-user/getUserIdListByDingTalkUserId',
        method: 'get',
        params: data,
    });
}

//绑定钉钉和师生通用户关系
export function bindDingTalkUserId(data) {
    return fetch({
        url: apiUrl + '/relative/ding-sst-user/bindDingTalkUserId',
        method: 'get',
        params: data,
    });
}
